package middleware

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"user/dao"
	"user/utils"
)

func AuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		//将用户的id传入auth
		header := c.GetHeader("Authorization")
		id, err := strconv.Atoi(header)
		if err != nil {
			utils.Fail(c, "接口请求信息错误")
			return
		}
		if id <= 0 {
			utils.Fail(c, "接口请求id信息错误")
			return
		}

		u := dao.GetUserInfoById(id)
		if u.ID == 0 {
			utils.Fail(c, "用户信息不存在")
			return
		}
		c.Set("user", u)
		c.Next()
	}
}
